//
//  HJStatusToolBar.swift
//  WeiBo
//
//  Created by 韩俊 on 15/12/11.
//  Copyright © 2015年 HanJun. All rights reserved.
//

import UIKit

class HJStatusToolBar: UIView {

    var retweetButton:UIButton?
    var commentButton:UIButton?
    var unlikeButton:UIButton?
    
    var statusViewModel:HJStatusViewModel?{
        
        didSet{
            
            retweetButton?.setTitle(statusViewModel?.retweetCountStr,forState: .Normal)
            commentButton?.setTitle(statusViewModel?.commentCountStr,forState: .Normal)
            unlikeButton?.setTitle(statusViewModel?.unlikeCountStr,forState: .Normal)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        retweetButton = addChildButton("转发",imgName: "timeline_icon_retweet")
        commentButton = addChildButton("评论",imgName: "timeline_icon_comment")
        unlikeButton = addChildButton("赞", imgName: "timeline_icon_unlike")
        
        //添加两个分割线
        let sp1 = addSeparator()
        let sp2 = addSeparator()
        
        // 添加约束
        retweetButton?.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(commentButton!)
        }
        
        commentButton?.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(retweetButton!.snp_trailing)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(unlikeButton!)
        }
        
        unlikeButton?.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.leading.equalTo(commentButton!.snp_trailing)
        }

        // 添加分割线的约束
        sp1.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(retweetButton!.snp_trailing)
            make.centerY.equalTo(self)
        }
        sp2.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(commentButton!.snp_trailing)
            make.centerY.equalTo(self)
        }

        
    }
    
    
    
    //添加三个button
    private func addChildButton(title:String, imgName:String) ->UIButton{
        
        let button = UIButton()
        button.setTitle(title, forState: .Normal)
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Highlighted)
        button.setImage(UIImage(named: imgName), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: .Highlighted)
        
        addSubview(button)
        
        return button
    }

    //添加分割线
    private func addSeparator() ->UIView {
        
        let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        addSubview(imageView)
        return imageView
    }
}
